package com.shijianchen.controller;


import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.shijianchen.projo.SpMenu;
import com.shijianchen.projo.vos.UserRoute;
import com.shijianchen.service.SpMenuService;
import com.shijianchen.utils.Result;
import com.shijianchen.utils.TreeElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/pro-api/auth/route")
public class MenuController {
    @Autowired
    private SpMenuService spMenuService;
    @GetMapping("/getUserRoutes")
    public Result<Object> getAuthRule() throws IllegalAccessException {
        List<TreeElement<UserRoute>> treeElement = spMenuService.getUserRoutes();
        HashMap<String, Object> map = new HashMap<>();
        map.put("home","home");
        map.put("routes",treeElement);
        return Result.ok(map);
    }
    @GetMapping("systemManage/menu/list")
    public Result<List<TreeElement<SpMenu>>> getMenuList() throws IllegalAccessException {
        List<TreeElement<SpMenu>> spMenuArrayList = spMenuService.getMenuList();
        return Result.ok(spMenuArrayList);
    }
    @PutMapping("systemManage/menu/{type}")
    public Result<Object> addOrUpdateMenu(@PathVariable String type,@RequestBody SpMenu spMenu){
        Integer result = spMenuService.addOrUpdateMenu(spMenu,type);
        if (result > 0){
            return Result.ok("操作成功");
        }
        return Result.ok("操作失败");
    }
    @DeleteMapping("systemManage/menu/remove/{id}")
    public Result<Object> removeMenuById(@PathVariable Long id){
        boolean result = spMenuService.removeById(id);
        if (result){
            return Result.ok("删除成功");
        }
        return Result.ok("删除失败");
    }
    @PostMapping("systemManage/menu/batchRemove")
    public Result<Object> batchRemove(@RequestBody List<Integer> ids){
        boolean result = spMenuService.removeBatchByIds(ids);
        if (result){
            return Result.ok("批量删除成功");
        }
        return Result.ok("批量删除失败");
    }
    @PostMapping("updateBatchMenu")
    public Result<Object> updateBatchMenu(@RequestBody ArrayList<SpMenu> spMenuArrayList){
        Boolean result = spMenuService.updateBatchMenu(spMenuArrayList);
        if(result){
            return Result.ok("批量添加成功");
        }
        return Result.ok("批量添加失败");
    }
    @GetMapping("systemManage/menu/detail/{id}")
    public Result<Object> queryMenuDetail(@PathVariable Long id){
        SpMenu query = spMenuService.getById(id);
        return Result.ok(query);
    }
}
